package com.njsf.utils;

<<<<<<< HEAD
import com.njsf.dto.ResponseJson;
import com.sun.org.apache.bcel.internal.generic.NEW;
=======
import com.google.gson.Gson;
import com.njsf.dto.ResponseJson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
<<<<<<< HEAD
import java.util.UUID;

public class UploadFile {

    public static ResponseJson upload(HttpServletRequest request, MultipartFile file) throws IOException {
        /*创建保存图片的文件夹*/
        String realPath = request.getServletContext().getRealPath("/upload");
        File dir = new File(realPath);
        if (!dir.exists()){
            dir.mkdirs();
        }
        /*获得文件原始名字*/
        String oldName = file.getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf('.') + 1);
        String newName = UUID.randomUUID().toString().replace("-","")+"."+suffix;
        file.transferTo(new File(realPath+File.separator+newName));
        return new ResponseJson(100,"上传成功!","upload/"+newName);
=======
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


public class UploadFile {

    private static final String urlPrefix = "http://sxcnm.qiangly.top/";

    private static final SimpleDateFormat sbf = new SimpleDateFormat("yyyyMMddHHmmss");

    private static final String accessKey = "EW6jbgWcO7Y3C3Kyx8M3p-1YPOzQmseHy6gyra76";
    private static final String secretKey = "uh5gOg-9fuygyNEUME_rxT6wVGuUloKFrtnJYErh";
    private static final String bucket = "sxcnm";

    public static ResponseJson upload(HttpServletRequest request, MultipartFile file) throws IOException {

        /*获得文件原始名字*/
        String oldName = file.getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf('.'));


        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String filename = sbf.format(new Date()) + suffix;

        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(file.getBytes(), filename, upToken);
            //解析上传成功的结果
            if (response.isOK()) {
                return new ResponseJson(100, "上传成功!", urlPrefix + filename);
            }
        } catch (QiniuException ex) {
            ex.printStackTrace();
        }
        return new ResponseJson(500, "上传失败!");
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
    }
}
